#ifndef CDBPROVIDER_H
#define CDBPROVIDER_H

#include <QtGui/QWidget>
#include <QSqlDatabase>
#include <QSqlQuery>
#include <QSqlQueryModel>
#include <QSqlRelation>
#include <QSqlRecord>
#include <QSqlField>
#include <QTreeWidget>
#include <QListWidget>

#include "mylistitem.h"
#include "mystringlist.h"

struct Blank{
    int shifr;
    bool a[25][4];
    QString b[10];
};

class CDBProvider : public QWidget
{
public:
    CDBProvider(QString path = "",QWidget * parent = 0);
    ~CDBProvider();
    QSqlQueryModel * tQuery;
    bool saveChieldToDB(QList<QTreeWidgetItem *> list,int index = 0);
    bool saveTreeWidgetItemToDB(QTreeWidgetItem * it, int index = 0);
    QList<QTreeWidgetItem *> getTestChieldFromDB(int index);
    QList<QTreeWidgetItem *> getTestFromDB(int index = 0);
    QList<QTreeWidgetItem *> getTaskChieldFromDB(int index, int k = 0);
    QList<QTreeWidgetItem *> getChieldFromDB(int index = 0, bool tasks = true,int  k=0);
    bool deleteItemFromDB(QTreeWidgetItem *);
    QString getStrinFromTree(int);
    bool deleteItemsFromDB(QList<QTreeWidgetItem *>);
    QList<MyListItem *> getAnswer(int taskId);
    void saveAnswer(MyListItem * i);
    bool wait;
    QString getTemplateAnswer(int idTask = 0);
    void saveTemplateAnswer(int, QString);
    bool rightAnswer(int,int);
    void saveRightAnswer(MyListItem * i);
    QStringList getSchools();
    bool exec(QString sql);
    QList <QTreeWidgetItem *> getGeneral();
    QString getSchool(int id);
    int getSchool(QString name);
    QList <int> getListTask(int id);
    int insertGeneral(QString,QString,int);
    int insertGeneralTasks(int);
    QList<QListWidgetItem *> getItemsForTasks(int id);
    QList<QTreeWidgetItem *> getTItemsForTasks(int id);
    void insertGeneralTasksTask(int id,QList <int> list);
    QList<int> getIdListTask(int id);
    QStringList getTaskForTasks(int id);
    void deleteGeneralTasks(int id);
    void renameSchool(QString,QString);
    QMap<QString,QString> getDataForTasks(int idTasks);
    void insertAnswerTester(Blank);
    QString getReportTotal(QStringList listId = QStringList());
    QString getReportTotalToTester(QStringList listId = QStringList());
    void deleteResult(int id);
    void deleteAnswerFromDB(int id);
    void taskSetOrder(int id, int order);
    QTreeWidgetItem * getGroupsFroGroup(int id,QTreeWidgetItem *root = 0);    
    QTreeWidgetItem * findIdfromGroup(QTreeWidgetItem * cs,  int id_group);
    bool delnullItem(QTreeWidgetItem * cs);
    QMap <int, QMap<int,bool> > getTasksTester(int id);
    QMap <int, QString > getTasksBTester(int id);
    void insertAnswerTesterA(int id,QList <int> id_answer);
    void insertAnswerTesterB(int id,QMap <int,QString > id_answer);
private:

    QSqlDatabase db;
    QSqlDatabase openDB();
};

#endif // CDBPROVIDER_H
